using System;
using OracleInternal.Common;

namespace Oracle.ManagedDataAccess.Client
{
	public sealed class OracleDataSourceCollection
	{
		internal static object s_syncObj = new object();

		public int Count
		{
			get
			{
				lock (s_syncObj)
				{
					return ConfigBaseClass.m_configDataSourcesMap.Count;
				}
			}
		}

		public string this[string tnsName]
		{
			get
			{
				lock (s_syncObj)
				{
					string text = "";
					if (ConfigBaseClass.m_configDataSourcesMap.Contains(tnsName))
					{
						text = ConfigBaseClass.m_configDataSourcesMap[tnsName]!.ToString();
					}
					if (!string.IsNullOrEmpty(text))
					{
						text = text.Trim();
					}
					return text;
				}
			}
			set
			{
				lock (s_syncObj)
				{
					if (OracleConnection.m_bAtLeastOneConOpen)
					{
						throw new InvalidOperationException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.CANNOT_MODIFY_ORACONFIG));
					}
					if (value != null)
					{
						ConfigBaseClass.m_configDataSourcesMap[tnsName] = value.Trim();
					}
					else
					{
						ConfigBaseClass.m_configDataSourcesMap[tnsName] = null;
					}
					if (ProviderConfig.m_bTraceLevelPublic)
					{
						Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Config, OracleTraceClassName.OracleDataSourceCollection, OracleTraceFuncName.Set, $": {tnsName} : {value}");
					}
				}
			}
		}

		internal OracleDataSourceCollection()
		{
		}

		public void Add(string tnsName, string tnsDescriptor)
		{
			lock (s_syncObj)
			{
				if (OracleConnection.m_bAtLeastOneConOpen)
				{
					throw new InvalidOperationException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.CANNOT_MODIFY_ORACONFIG));
				}
				ConfigBaseClass.m_configDataSourcesMap[tnsName] = tnsDescriptor.Trim();
				if (ProviderConfig.m_bTraceLevelPublic)
				{
					Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Config, OracleTraceClassName.OracleDataSourceCollection, OracleTraceFuncName.Add, $": {tnsName} : {tnsDescriptor.Trim()}");
				}
			}
		}

		public void Remove(string tnsName)
		{
			lock (s_syncObj)
			{
				if (OracleConnection.m_bAtLeastOneConOpen)
				{
					throw new InvalidOperationException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.CANNOT_MODIFY_ORACONFIG));
				}
				ConfigBaseClass.m_configDataSourcesMap.Remove(tnsName);
				if (ProviderConfig.m_bTraceLevelPublic)
				{
					Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Config, OracleTraceClassName.OracleDataSourceCollection, OracleTraceFuncName.Remove, $": {tnsName}");
				}
			}
		}
	}
}
